package mCalculator;

import ewe.fx.mImage;
import ewe.ui.CellConstants;
import ewe.ui.Event;
import ewe.ui.EventListener;
import ewe.ui.PenEvent;
import ewe.ui.mButton;
import ewe.ui.Editor;
import ewe.ui.mLabel;
import ewe.ui.mInput;
import ewe.ui.SingleContainer;
import ewe.ui.Panel;
import funciones.Basica;

public class Main extends Editor {
	private mButton digito[];	
	
	private mButton ce = new mButton(new mImage("datos/clr.png"));
	private mButton undo = new mButton(new mImage("datos/undo.png"));
	private mButton menosuno = new mButton(new mImage("datos/menosuno.png"));
	private mButton bPunto= new mButton(new mImage("datos/punto.png"));
	private mButton bIgual = new mButton(new mImage("datos/igual.png"));
	private mButton bMultiplicar = new mButton(new mImage("datos/multi.png"));
	private mButton bDividir= new mButton(new mImage("datos/divi.png"));
	private mButton bMas= new mButton(new mImage("datos/mas.png"));
	private mButton bMenos = new mButton(new mImage("datos/menos.png"));
	private mButton bFraccion = new mButton(new mImage("datos/frac.png"));				
	private String arg1="0";
	private String arg2="0";
	private char oper='+';
	private Basica basica;		
	private SingleContainer contenedor = new SingleContainer();
	private mInput display = new mInput("0");			
	private void Digitos() {
		
		Panel panel= new Panel();	
		digito = new mButton[10];		
		ce.addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					display.setText("0");
					arg1 = arg2 = "0";
					oper = '+';
				}
			}
		}
		);		
		menosuno.addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					if(display.getText().length()>0) {
						if(display.getText().charAt(0)=='-') {
							display.setText(display.getText().substring(1,display.getText().length()));
						}
						else {
							display.setText("-"+display.getText());
						}
					}					
				}
			}
		});
		
		undo.addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					if(display.getText().length()>0) {
						
						if(display.getText().length()==1)
							display.setText("0");
						else
							display.setText(display.getText().substring(0,display.getText().length()-1));
					}					
				}
			}
		}
		);
		
		
		for(int i=9; i>=0; i--) { 
			digito[i] = new mButton(new mImage("datos/"+i+".png"));			
		}
		digito[0].addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					fijarDisplay("0");					
				}
			}
		});
		digito[1].addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					fijarDisplay("1");
				}
			}
		});
		digito[2].addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					fijarDisplay("2");
				}
			}
		});
		digito[3].addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					fijarDisplay("3");
				}
			}
		});
		digito[4].addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					fijarDisplay("4");
				}
			}
		});
		digito[5].addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					fijarDisplay("5");
				}
			}
		});
		digito[6].addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					fijarDisplay("6");
				}
			}
		});
		digito[7].addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					fijarDisplay("7");
				}
			}
		});
		digito[8].addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					fijarDisplay("8");
				}
			}
		});
		digito[9].addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					fijarDisplay("9");
				}
			}
		});
		
		
		bPunto.addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					if(validarPunto(display.getText())) {
						display.setText(display.getText()+".");
					}
					/*
					if(punto) {
						display.setText(display.getText()+".");
						punto=false;
					}
					*/
				}
			}
		});
		bIgual.addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {					
					arg2 = display.getText();
					basica.set(arg1, oper, arg2);
					display.setText( basica.evaluarSimbolicamente() );
					if(oper!='=')
						oper = '=';					
				}
			}
		});
		bMultiplicar.addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					oper = '*';
					arg1 = display.getText();
					display.setText("0");
				}
			}
		});
		bDividir.addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					oper = '/';
					arg1 = display.getText();
					display.setText("0");
				}
			}
		});
		bMas.addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					oper = '+';
					arg1 = display.getText();
					display.setText("0");
				}
			}
		});
		bMenos.addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					oper = '-';
					arg1 = display.getText();
					display.setText("0");
				}
			}
		});
		bFraccion.addListener(new EventListener() {
			public void onEvent(Event ev){
				if (ev.type == PenEvent.PEN_DOWN) {
					if(!buscarDisplay('/')) {
						if(Double.valueOf(display.getText())==0)
							display.setText("0/");		
						else
							display.setText(display.getText()+"/");		
					}
				}
			}
		});
															
		panel.addNext(ce).setCell(STRETCH);					panel.addNext(bFraccion).setCell(STRETCH);		panel.addNext(menosuno).setCell(STRETCH); 		panel.addLast(bMultiplicar).setCell(STRETCH);
		panel.addNext(digito[7]).setCell(STRETCH);			panel.addNext(digito[8]).setCell(STRETCH); 			panel.addNext(digito[9]).setCell(STRETCH); 		panel.addLast(bDividir).setCell(STRETCH);
		panel.addNext(digito[4]).setCell(STRETCH); 			panel.addNext(digito[5]).setCell(STRETCH); 			panel.addNext(digito[6]).setCell(STRETCH); 		panel.addLast(bMas).setCell(STRETCH);
		panel.addNext(digito[1]).setCell(STRETCH);			panel.addNext(digito[2]).setCell(STRETCH); 			panel.addNext(digito[3]).setCell(STRETCH);	 	panel.addLast(bMenos).setCell(STRETCH);
		panel.addNext(new mLabel("")).setCell(STRETCH);			panel.addNext(digito[0]).setCell(STRETCH); 			panel.addNext(bPunto).setCell(STRETCH); 		panel.addNext(bIgual).setCell(STRETCH); 	
		contenedor.setControl(panel, true);
	}
	public void fijarDisplay(String s) {
		if(buscarDisplay('/')) {
			display.setText(display.getText()+s);			
		}else if( (Double.valueOf(display.getText())==0)&&(!buscarDisplay('.')) ) 
				display.setText(s);				
		else
			display.setText(display.getText()+s);
	}
	private boolean buscarDisplay(char c) {
		for(int i=0; i< display.getText().length(); i++  )
			if(display.getText().charAt(i)==c)
				return true;
		return false;
	}
	private boolean validarPunto(String s) {
		
		boolean po = false;
		if(buscarDisplay('/')) {
			for(int i=0; i<s.length(); i++) {
				if(s.charAt(i)=='.') {
					if(po)
						return false;
					po =  true;
				}
			}
		} else {
			for(int i=0; i<s.length(); i++) {
				if(s.charAt(i)=='.') {					
					return false;					
				}
			}
		}
		return true;
	}
	public Main() {
		
		title = "mCalculator";		
		setPreferredSize(245,300);
		display.setPreferredSize(185, 10);
		
		addNext(new mLabel(""));		
		addNext(display,CellConstants.MAXIMUMSIZE,CellConstants.FILL);		
		addLast(undo,CellConstants.HSTRETCH,CellConstants.DONTFILL);
		addLast(new mLabel(""));		
		addLast(contenedor);
		Digitos();
		basica = new Basica(arg1,oper,arg2);				
	}
}
